<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/7
 * Time: 18:48
 */

namespace app\controllers;
use app\components\DealOrder;
use app\models\UserPet;
use yii\web\Controller;
use Yii;

class OrderController extends Controller
{
    /**
     * 购买宠物
     * @return \yii\web\Response
     */
    public function actionAddPet()
    {
        if (Yii::$app->user->isGuest) {
            return $this->asJson(['code' => 10000, 'msg' => '您还没有登录']);
        }
        $puid = Yii::$app->request->post('puid');
        $userPet = new UserPet();
        if (empty($puid)) {
            return $this->asJson(['code' => 10001, 'msg' => '非法传参']);
        }
        $userPetRow = $userPet->find()->where('puid=:puid and ascription_status=1 and auction_status=1',
            [':puid' => $puid])->with('pet')->one();
        if (empty($userPetRow)) {
            return $this->asJson(['code' => 10002, 'msg' => '玩客猪已取消拍卖或被别的玩家买走']);
        }
        $DealOrder = new DealOrder();
        $DealOrder->setOrder($userPetRow, 'withPet');
        $result = $DealOrder->addOrder();
        if ($result['code'] != 10000) {
            return $this->asJson($result);
        }
        return $this->asJson(['code' => 10000, 'msg' => '购买成功']);
    }
}